import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import CreateArticle from '../components/CreateArticle/CreateArticle'
import ModifyArticle from '../components/modifyArticle/modifyArticle'
import CreateJournal from '../components/CreateJournal/CreateJournal'
import ModifyJournal from '../components/modifyJournal/modifyJournal'
import EditingClass from '../components/editingClass/EditingClass'
import EditPictur from '../components/EditPictur/EditPictur'
import AddUser from '../components/addUser/addUser'
import About from '../components/About/About'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/Login' },
  { path: '/Login', component: Login },
  {
    path: '/Home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/create', component: CreateArticle },
      { path: '/modifyArticle', component: ModifyArticle },
      { path: '/createJournal', component: CreateJournal },
      { path: '/Journal', component: ModifyJournal },
      { path: '/editClass', component: EditingClass },
      { path: '/editPictur', component: EditPictur },
      { path: '/addUser', component: AddUser },
      { path: '/about', component: About }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 要访问的路径
  // from 从那个路径跳转而来
  // next 是一个函数，表示放行
  // next() 放行
  // next('/login') 强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
